package com.hzqy.service.sensitive;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.hzqy.commons.utils.ConvertBeanUtil;
import com.hzqy.commons.utils.ExcelUtil;
import com.hzqy.commons.utils.sensitive.SensitiveMain;
import com.hzqy.commons.utils.sensitive.SensitiveWordFilter;
import com.hzqy.persistence.dao.sensitive.SensitiveWordsDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.SysSensitiveWordsVo;

@Service
public class SensitiveWordsServiceBean extends GenericServiceBean<SysSensitiveWordsVo,SensitiveWordsDao,Integer>{
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private SensitiveWordsDao sensitiveWordsDao;
	
	@Override
	public SensitiveWordsDao getEntityDao() {
		return sensitiveWordsDao;
	}
	/**
	 * 敏感词列表获取
	 * @param SysSensitiveWordsVo
	 * @param curPage
	 * @param pageSize
	 * @return
	 */
	public Map<String,Object> selectSensitiveList(SysSensitiveWordsVo sysSensitiveWordsVo, int curPage,
			int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = sensitiveWordsDao.findSensitiveCount(sysSensitiveWordsVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_ssw_name", sysSensitiveWordsVo.getF_ssw_name());
			map.put("f_ssw_status", sysSensitiveWordsVo.getF_ssw_status());
			List<SysSensitiveWordsVo> results = sensitiveWordsDao.findSensitiveList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<CmContentVo>());
		}
		return map;
	}
	
	/**
	 * 增加敏感词---批量入库和单条入库都利用此方法
	 * @param contentVo
	 * @return
	 */
	public int insertSensitive(SysSensitiveWordsVo sysSensitiveWordsVo) {
		int flag = 0;
		if(sysSensitiveWordsVo==null || sysSensitiveWordsVo.getF_ssw_name()==null) {
			flag = -1;
		}
		if(flag ==0) {
			int f = sensitiveWordsDao.insertSensitive(sysSensitiveWordsVo);
			return 1;
		}
		return flag;
	}
	
	/**
	 * 删除敏感词
	 * @param f_ssw_id
	 * @return
	 */
	public int deleteSensitive(int f_ssw_id) {
		sensitiveWordsDao.deleteSensitive(f_ssw_id);
		return 1;
	}
	
	/**
	 * 根据id查询敏感词详情
	 * @param f_ssw_id
	 * @return
	 */
	public SysSensitiveWordsVo selectSensitive(int f_ssw_id) {
		return sensitiveWordsDao.selectSensitive(f_ssw_id);
	}
	
	/**
	 * 修改敏感词
	 * @param sysSensitiveWordsVo
	 * @return
	 */
	public int updateSensitive(SysSensitiveWordsVo sysSensitiveWordsVo) {
		int flag = 0;
		if(sysSensitiveWordsVo==null || sysSensitiveWordsVo.getF_ssw_name()==null) {
			flag = -1;
		}
		if(flag ==0) {
			sensitiveWordsDao.updateSensitive(sysSensitiveWordsVo);
			return 1;
		}
		return flag;
	}
	
	/**
	 * 敏感词审核
	 * @param cmContentVo
	 * @return
	 */
	public int reviewSensitive(SysSensitiveWordsVo sysSensitiveWordsVo) {
		int flag = 0;
		if(sysSensitiveWordsVo==null) {
			flag = -1;
		}
		if(flag == 0) {
			sensitiveWordsDao.reviewSensitive(sysSensitiveWordsVo);
			return 1;
		}
		return flag;
	}
	
	public int batchDeleteSensitive(String ids) {
		int flag = 0;
		String[] idsArr = ids.split(",");
		for(int i=0;i<idsArr.length;i++) {
			flag += sensitiveWordsDao.deleteSensitive(Integer.valueOf(idsArr[i]));
		}
		return flag;
	}
	
	public int batchReviewSensitive(String ids) {
		int flag = 0;
		String[] idsArr = ids.split(",");
		for(int i=0;i<idsArr.length;i++) {
			SysSensitiveWordsVo sysSensitiveWordsVo = new SysSensitiveWordsVo();
			sysSensitiveWordsVo.setF_ssw_id(Integer.valueOf(idsArr[i]));
			sysSensitiveWordsVo.setF_ssw_status(1);
			flag += sensitiveWordsDao.reviewSensitive(sysSensitiveWordsVo);
		}
		return flag;
	}
	
	public int batchUnReviewSensitive(String ids) {
		int flag = 0;
		String[] idsArr = ids.split(",");
		for(int i=0;i<idsArr.length;i++) {
			SysSensitiveWordsVo sysSensitiveWordsVo = new SysSensitiveWordsVo();
			sysSensitiveWordsVo.setF_ssw_id(Integer.valueOf(idsArr[i]));
			sysSensitiveWordsVo.setF_ssw_status(2);
			flag += sensitiveWordsDao.reviewSensitive(sysSensitiveWordsVo);
		}
		return flag;
	}
	
	public List<String> selectAllSevsitiveWords(Map<String, Object> map){
		return sensitiveWordsDao.selectAllSevsitiveWords(map);
	}
	
	public int refreshSensitiveWord() {
		Map<String,Object> map = new HashMap<String, Object>();
		List<String> sesitiveWordList = this.selectAllSevsitiveWords(map);
		SensitiveWordFilter filter = SensitiveWordFilter.getInstance();
		filter.resetSensitiveWord(sesitiveWordList);
		LOG.debug("刷新敏感词缓存，本次刷新获取敏感词: " + sesitiveWordList.size());
		return 1;
	}
	
	public String insertBatchContent(MultipartFile file){
		int success = 0;
		StringBuffer str = new StringBuffer();
		StringBuffer str2 = new StringBuffer();
		InputStream is= null;
		try {
			is = file.getInputStream();
			LOG.debug("批量导入敏感词开始");
			List<Map<String, Object>> list = ExcelUtil.parseExcel(file.getInputStream(), ExcelUtil.ALLOWED_EMPTY_MODE, new String[]{});
			LOG.debug("解析excel完成, 开始循环入库");
			//10年后
			SysSensitiveWordsVo ssw = new SysSensitiveWordsVo();
			for(int i=0;i<list.size();i++){
				try {
					Map<String, Object> m = list.get(i);
					SysSensitiveWordsVo sysSensitiveWordsVo = ConvertBeanUtil.getBeanFromMap(m, SysSensitiveWordsVo.class, SysSensitiveWordsVo.class.getName());
					int flag = 0;
					ssw.setF_ssw_name(sysSensitiveWordsVo.getF_ssw_name());
					ssw.setF_ssw_status(1);
					flag = insertSensitive(ssw);
					
					success += (flag > 0 ? 1 : 0);
					if(flag <= 0&&ssw.getF_ssw_name()!=null) {
						str2.append(i+1).append(",");
					}
				} catch (Exception e) {
					LOG.error("", e);
					str.append("敏感词重复: ").append(ssw.getF_ssw_name()).append("<br/>");
				}
			}
			LOG.debug("批量完成");
			if(str2.length() > 0) {
				str.append("第").append(str2.substring(0, str2.length() - 1)).append("条导入失败! ").append("<br/>");
			}
			str.append("批量导入完成， 一共：").append(list.size()).append("条数据, 成功").append(success).append(", 失败").append(list.size() - success);
		} catch (Exception e) {
			LOG.error("", e);
			//str.append("批量导入失败: " + e.getMessage());
		}finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
				}
			}
		}
		
		return str.toString();
	}
	public Map<String, Object> checkSensitiveWord(String txt, int type) {
		return SensitiveMain.sensitiveWordCheck(txt, type);
	}
}
